//File System Operations
#include "../include/constant.h"
#include <stdio.h>


/**Method to create a file.
Name and size of the vfs will be entered by the user.
Allocating memory for Meta-Header, Header and Blocks**/

void create_vfs(m_header a, int size) 
{
	 FILE *fp;
	 int metaSize,sizeLeft,num,headerSize,userspace,i;
	 
	 fp=fopen("/home/mridul/vfs-group-4/svn/bin/vfs.bin","wb+");
	 if(fp==NULL)
	 printf("\n File is not created!!!");
	 else
	 {
		 metaSize=sizeof(a.fsLabel) + sizeof(a.usedfd);
		 sizeLeft=size*1024*1024-metaSize;
		 // num contains total no. of blocks created which is equal to no. of file descriptor and added 1 for the array containing 				block status
		 num=sizeLeft/(sizeof(f_desc)+sizeof(blk)+1);
		 headerSize=num*(sizeof(f_desc))+num; //num added which have status for all the blocks created
		 userspace=sizeLeft-headerSize;
		 a.maxfd=num; a.usedfd=0;
		 fwrite(&a.fsLabel,sizeof(a.fsLabel),1,fp);
		fwrite(&a,sizeof(a),1,fp);
		fwrite(&userspace,userspace,1,fp);

		 
		 fclose(fp);
		 printf("\n****** VFS CREATED ******");
		 printf("\nTotal number of filedescriptors created: %d\n",num);
		 printf("\nMemory used by Meta Header %d bytes", metaSize);
		 printf("\n Memory used by Header %d bytes", headerSize);
		 printf("\n Memory used by userspace %d bytes", userspace);
	 }

}
